'''
    Goblinoid: Experience all of MediaGoblin on an Android Device
    Copyright (C) 2015 Dylan Jeffers

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
'''

from __future__ import print_function
from os.path import join

from kivy.app import App
from kivy.core.window import Window
from kivy.uix.screenmanager import ScreenManager, SlideTransition
from kivy.utils import platform
from pypump.exception import PyPumpException

from goblinoid.services.pump_service import PumpService

from goblinoid.services.login_service import user_logged_on, load_user


# Note: this versioning is for kivy, not Goblinoid
__version__ = '1.9.0'


class MediaGoblinApp(App):

    def build(self, **kwargs):
        self.pump_service = PumpService()
        self.transition = SlideTransition(duration=.35)
        self.sm = ScreenManager(transition=self.transition)
        self.user_dir = join(self.user_data_dir, 'MediaGoblin')
        # Note: this is a horrible hotfix to avoid the common
        # PyPumpException. This is next on the priority list to fix
        if user_logged_on(self.user_dir):
            try:
                pump = load_user(self.user_dir)
                self.pump_service.pump = pump
                self.open_view('action', False)
            except PyPumpException, e:
                print('Error: {0}, trying once more...'.format(e))
                # Note: eventually add a toast to alert user of issue
                try:
                    pump = load_user(self.user_dir)
                    self.pump_service.pump = pump
                    self.open_view('action', False)
                except PyPumpException, e:
                    print('Error: {0}, transitioning to login'.format(e))
                    self.open_view('login', True)
        else:
            self.open_view('login', False)

        self.bind(on_start=self.post_build_init)
        return self.sm

    def post_build_init(self, ev):
        # map Android keys
        if platform == 'android':
            Window.softinput_mode = 'below_target'
            # will eventually add key_handler to deal with back button
        else:
            Window.softinput_mode = 'pan'

    def update_pump_service(self, pump):
        self.pump_service.update_pump(pump)

    def open_view(self, name, previous):
        print('got to open_view: {0}, {1}'.format(name, previous))
        if self.sm.has_screen(name):
            self.sm.remove_widget(self.sm.get_screen(name))
        # grabs screen from dictionary of screens
        screen = self.pump_service.screen_dict[name](name=name)
        self.sm.add_widget(screen)
        if previous:
            self.transition.direction = 'right'
        else:
            self.transition.direction = 'left'
        self.sm.current = screen.name

    # ask about private data members
    def get_pump_service(self):
        print('printing the pump model: {0}'.format(self.pump_service))
        return self.pump_service

    # handlers for when user exits from app
    def on_pause(self):
        return True

    def on_resume(self):
        pass


if __name__ == '__main__':
    MediaGoblinApp().run()
